package com.nuanshui.heatedloan.controller.display.credit;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.framework.security.DESC;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.controller.BaseController;
import com.nuanshui.heatedloan.credit.facade.ApplyOrderService;
import com.nuanshui.heatedloan.credit.facade.ExecuteCustomAuthService;
import com.nuanshui.heatedloan.credit.facade.PayOrderInfoService;
import com.nuanshui.heatedloan.credit.facade.job.PayOrderJobService;
import com.nuanshui.heatedloan.credit.facade.third.party.CreditCardAuthService;
import com.nuanshui.heatedloan.credit.facade.third.party.JingDongAuthService;
import com.nuanshui.heatedloan.credit.facade.third.party.PhoneNumAuthService;
import com.nuanshui.heatedloan.credit.facade.third.party.SesameCreditAuthService;
import com.nuanshui.heatedloan.credit.form.CustomerCreditCardInfoForm;
import com.nuanshui.heatedloan.credit.form.CustomerJingDongInfoForm;
import com.nuanshui.heatedloan.credit.form.CustomerPhoneInfoForm;
import com.nuanshui.heatedloan.credit.form.CustomerSesameCreditInfoForm;
import com.nuanshui.heatedloan.credit.response.CalculationLimitResponse;
import com.nuanshui.heatedloan.credit.response.ClientResponse;
import com.nuanshui.heatedloan.credit.response.RiskResponse;
import com.nuanshui.heatedloan.credit.vo.CustomerCreditCardInfoVO;
import com.nuanshui.heatedloan.credit.vo.CustomerJingDongInfoVO;
import com.nuanshui.heatedloan.credit.vo.CustomerPhoneInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangxw on 2017/6/29.
 */

@RequestMapping("credit/auth")
@RestController
public class CustomerAuthController extends BaseController {

    @Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout= GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
    private ExecuteCustomAuthService executeCustomAuthService;

    @Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout= GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
    private SesameCreditAuthService sesameCreditAuthService;

    @Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout= GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
    private PhoneNumAuthService phoneNumAuthService;

    @Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout= GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
    private JingDongAuthService jingDongAuthService;

    @Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout= GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
    private CreditCardAuthService creditCardAuthService;

    private Logger logger = LoggerFactory.getLogger(CustomerAuthController.class);

    @Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout= GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
    private ApplyOrderService applyOrderService;

    @Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout= GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
    private PayOrderInfoService payOrderInfoService;

    @Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout= GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
    private PayOrderJobService payOrderJobService;



    @RequestMapping(value = "/getStatus",method = RequestMethod.POST)
   public String getCustomAuthStatus(int type){
       Map<String,Object> map=new HashMap<String,Object>();
       String userId=getCurrentUser().getId();
       map=executeCustomAuthService.getCustomAuthStatus(userId,type);
       return  success(map);
   }

    /***
     * 芝麻认证
     * @param customerSesameCreditInfoStr
     * @return
     */
    @RequestMapping(value = "/sesame",method = RequestMethod.POST)
   public String sesameCreditInfoAuth(String customerSesameCreditInfoStr){
        String userId=getCurrentUser().getId();
        CustomerSesameCreditInfoForm customerSesameCreditInfoForm=new CustomerSesameCreditInfoForm();
        customerSesameCreditInfoForm.setUserId(userId);
        Map<String,Object> maps=sesameCreditAuthService.sesameCreditAuth(customerSesameCreditInfoForm);
        return success(maps);
   }

    /***
     * 把芝麻认证数据置为无效
     * @return
     */
   @RequestMapping(value = "/removeSesame",method = RequestMethod.POST)
   public String removeSesameAuthStatus(){
       String userId=getCurrentUser().getId();
       sesameCreditAuthService.updateSesameStatus(0,userId);
       return success();
   }

    /***
     * 芝麻信用回调的地址  把数据通过该接口返回
     * @param data
     * @return
     */
    @RequestMapping(value = "/callBackSesame",method = RequestMethod.POST)
   public String getSesameCreditAuthCallBack(@RequestBody String data){
        logger.info("====================接受参数为" + data + "=============================");
        RiskResponse riskResponse=new RiskResponse();
        riskResponse = JSON.parseObject(data, RiskResponse.class);
        sesameCreditAuthService.getSesameCreditAuthCallBack(riskResponse);
       return success();
   }



    /***
     * 手机认证
     * @param customerPhoneInfoStr
     * @return
     */
    @RequestMapping(value = "/phone",method = RequestMethod.POST)
   public String customerPhoneInfoAuth(String customerPhoneInfoStr,String smsCode){
        String userId=getCurrentUser().getId();
        if(SysStringUtils.isEmpty(customerPhoneInfoStr)){
            return error("请传入参数");
        }
        CustomerPhoneInfoForm customerBankCardInfoForm= JSON.parseObject(customerPhoneInfoStr,CustomerPhoneInfoForm.class);
        String phone= DESC.decrypt3DES(customerBankCardInfoForm.getPhone());
        String phoneUser=getCurrentUser().getPhone();
        if(!phone.equals(phoneUser)){
            error("认证手机号和注册手机号不一致");
        }
        List<CustomerPhoneInfoVO> customerPhoneInfoVOs=new ArrayList<CustomerPhoneInfoVO>();
        customerPhoneInfoVOs=phoneNumAuthService.loadAllVO();
        for(CustomerPhoneInfoVO customerPhoneInfoVO:customerPhoneInfoVOs){
            if(phone.equals(customerPhoneInfoVO.getPhone())){
                error("该号码已经认证过");
            }
        }
        //String passwd=DESC.decrypt3DES(customerBankCardInfoForm.getPassword());
        customerBankCardInfoForm.setPhone(phone);
        //customerBankCardInfoForm.setPassword(passwd);
        customerBankCardInfoForm.setStatus(1);
        customerBankCardInfoForm.setUserId(userId);
        RiskResponse riskResponse=phoneNumAuthService.mobilePhoneAuth(customerBankCardInfoForm,smsCode);
       return success(riskResponse);
   }

    /***
     * 京东认证
     * @param customerJingDongStr
     * @return
     */
    @RequestMapping(value = "/jingDong",method = RequestMethod.POST)
   public String customerJingDongInfoAuth(String customerJingDongStr){
        String userId=getCurrentUser().getId();
        if(SysStringUtils.isEmpty(customerJingDongStr)){
            return error("请传入参数");
        }
        CustomerJingDongInfoForm customerJingDongInfoForm=JSON.parseObject(customerJingDongStr,CustomerJingDongInfoForm.class);
        String userName=DESC.decrypt3DES(customerJingDongInfoForm.getUserName());
        List<CustomerJingDongInfoVO> customerJingDongInfoVOs=new ArrayList<CustomerJingDongInfoVO>();
        customerJingDongInfoVOs=jingDongAuthService.loadVOAll();
        for(CustomerJingDongInfoVO customerJingDongInfoVO:customerJingDongInfoVOs){
            if(customerJingDongInfoVO.getUserName().equals(userName)){
                return error("该账号已经认证");
            }
        }
        customerJingDongInfoForm.setUserName(userName);
        customerJingDongInfoForm.setStatus(1);
        customerJingDongInfoForm.setUserId(userId);
        Map<String,Object> maps=jingDongAuthService.jingDongAuth(customerJingDongInfoForm);
    return success(maps);
   }

    /***
     * 信用卡认证
     * @param customerCreditCardAuthStr
     * @return
     */
    @RequestMapping(value = "/creditCard",method = RequestMethod.POST)
   public String customerCreditCardAuth(String customerCreditCardAuthStr){
       String userId=getCurrentUser().getId();
       if(SysStringUtils.isEmpty(customerCreditCardAuthStr)){
           return error("请传入参数");
       }
       CustomerCreditCardInfoForm customerCreditCardInfoForm=JSON.
               parseObject(customerCreditCardAuthStr,CustomerCreditCardInfoForm.class);
       String cardNum=DESC.decrypt3DES(customerCreditCardInfoForm.getCreditCardNum());
       customerCreditCardInfoForm.setCreditCardNum(cardNum);
       customerCreditCardInfoForm.setStatus(1);
        customerCreditCardInfoForm.setUserId(userId);
        if(customerCreditCardInfoForm.getCreditCardTime()==null){
            return error("请传信用卡有效期");
        }
        List<CustomerCreditCardInfoVO> customerCreditCardInfoVOs=new ArrayList<CustomerCreditCardInfoVO>();
        customerCreditCardInfoVOs=creditCardAuthService.loadCreditCardAll();
        for(CustomerCreditCardInfoVO customerCreditCardInfoVO:customerCreditCardInfoVOs){
            if(cardNum.equals(customerCreditCardInfoVO.getCreditCardNum())){
                return error("该卡已经认证过");
            }
        }
        Map<String,Object> maps=creditCardAuthService.creditCardAuth(customerCreditCardInfoForm);
       return success(maps);
   }

    @RequestMapping(value = "/calculationLimit",method = RequestMethod.POST)
   public String calculationLimit(String appList,String deviceInfoStr ){
        String userId=getCurrentUser().getId();
        executeCustomAuthService.calculationLimit(userId);
       return success();
   }
    @RequestMapping(value = "/callBackCalculationLimit",method = RequestMethod.POST)
   public String callBackCalculationLimit(@RequestBody String data){
        logger.info("====================接受参数为" + data + "=============================");
        CalculationLimitResponse calculationLimitResponse=JSON.parseObject(data, CalculationLimitResponse.class);
        executeCustomAuthService.sendMqCallBackCalculationLimit(calculationLimitResponse);
     return success();
   }

    @RequestMapping(value = "/isWhiteList",method = RequestMethod.POST)
    public String isWhiteList(String phone){
        String userId=getCurrentUser().getId();
        ClientResponse clientResponse=executeCustomAuthService.isWhiteList(phone,userId);
        return success(clientResponse);
    }



}
